package com.itheima.exceptionResolver;

import org.apache.commons.lang3.SerializationException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 郭文亮
 * @since 2021/10/28 19:10
 */
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse,
                                         Object o,
                                         Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        // 超出上线异常
        if (e instanceof MaxUploadSizeExceededException) {
            modelAndView.addObject("msg","上传失败，请联系管理员");
        } else if (e instanceof SerializationException) {
            modelAndView.addObject("msg","实体类为序列化");
        } else {
            modelAndView.addObject("msg","未知异常"+e.getMessage());
        }
        modelAndView.setViewName("/error.jsp");
        return modelAndView;
    }
}
